import { SSE } from "sse.js";
import * as R from "ramda";

const connect = R.curryN(5, _connect);

function _connect(onMessage, onError, url, headers, data) {
  let sse;
  sse = new SSE(url, { headers }, data);
  sse.addEventListener("message", onMessage);
  sse.addEventListener("error", onError);

  return sse;
}

function createSSE(onMessage, onError = R.identity) {
  return connect(R.__, R.__, R.__, onMessage, onError);
}
export default createSSE;
